package controllers;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import security.Authority;
import security.UserAccount;
import services.AdministratorService;
import services.UserAccountService;
import domain.Administrator;
import forms.AdministratorForm;

@Controller
@RequestMapping("/account/administrator")
public class AdministratorAccountController extends AbstractController{
	
	// Constructors -----------------------------------------------------------

		public AdministratorAccountController() {
			super();
		}

	// Services ---------------------------------------------------------------
		
		@Autowired
		private UserAccountService userAccountService;
		
	
		@Autowired
		private AdministratorService administratorService;
				
		
		@RequestMapping(value = "/create", method = RequestMethod.GET)
		public ModelAndView editAdministrator() {
			ModelAndView result;
			UserAccount u = userAccountService.create();
			Authority a = new Authority();
			a.setAuthority("ADMIN");
			u.addAuthority(a);
			Administrator e = administratorService.create();
			e.setUserAccount(u);
			AdministratorForm administratorForm = administratorService.getFormObject(e);
			result = createEditModelAndView(administratorForm);

			return result;
		}
		
		
		
		protected ModelAndView createEditModelAndView(AdministratorForm e) {
			ModelAndView result;

			result = createEditModelAndView(e, null);
			
			return result;
		}	
		
		protected ModelAndView createEditModelAndView(AdministratorForm e, String message) {
			ModelAndView result;;
			result = new ModelAndView("account/new");
			result.addObject("administratorForm",e);
			result.addObject("requestURI", "account/administrator/edit.do");
			result.addObject("type","administratorForm");
			result.addObject("message",message);
			return result;
		}
		
//		@RequestMapping(value = "/edit", method = RequestMethod.POST, params = "save")
//		public ModelAndView saveAdministrator(@Valid Administrator administrator,
//				BindingResult binding) {
//			ModelAndView result;
//			
//			if (binding.hasErrors()) {
//				result = createEditModelAndView(administrator);
//
//			} else {
//				try {
//					
//					administratorService.save(administrator);
//					SimpleDateFormat formatter;
//					String moment;
//					result = new ModelAndView("welcome/index");
//					formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm");
//					moment = formatter.format(new Date());
//					result.addObject("moment", moment);
//					result.addObject("message", "account.commit.ok");
//		
//				} catch (Throwable oops) {
//					result = createEditModelAndView(administrator,
//							"account.commit.error");
//
//				}
//
//			}
//
//			return result;
//		}

		@RequestMapping(value = "/edit", method = RequestMethod.POST, params = "save")
		public ModelAndView saveAdministrator(@Valid AdministratorForm administratorForm, BindingResult binding) {
			ModelAndView result;
			
			if(!administratorForm.getRepeatedPassword().equals(administratorForm.getUserAccount().getPassword()))
				result = createEditModelAndView(administratorForm, "account.passwords.notmatch");
			else if (!administratorForm.getHasAccepted())
				result = createEditModelAndView(administratorForm, "account.legal.checked");
			else{
				
				if (binding.hasErrors()) {
					result = createEditModelAndView(administratorForm);
					}
				else{
				try {
					Administrator administrator = administratorService.reconstruct(administratorForm);
					administratorService.save(administrator);
					SimpleDateFormat formatter;
					String moment;
					result = new ModelAndView("welcome/index");
					formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm");
					moment = formatter.format(new Date());
					result.addObject("moment", moment);
					result.addObject("message", "account.commit.ok");
					
				} catch (Throwable oops) {
					result = createEditModelAndView(administratorForm,
							"account.commit.error");
				}
			}
			}
			return result;
		}
		
}
